import tkinter as tk
from tkinter import messagebox
from alg1 import Solution

class WiggleSortApp:
    def __init__(self, root):
        self.root = root
        self.root.title("摆动排序工具")
        
        # 输入框
        self.label = tk.Label(root, text="输入数组（用逗号分隔）:")
        self.label.pack(pady=10)
        
        self.entry = tk.Entry(root, width=60)
        self.entry.pack(pady=5)
        
        # 排序按钮
        self.sort_button = tk.Button(root, text="排序", command=self.perform_sort)
        self.sort_button.pack(pady=10)
        
        # 结果显示
        self.result_label = tk.Label(root, text="排序结果:")
        self.result_label.pack(pady=5)
        
        self.result_text = tk.Text(root, height=4, width=60, state="disabled")
        self.result_text.pack(pady=10)
        
        # 清空按钮
        self.clear_button = tk.Button(root, text="清空", command=self.clear_fields)
        self.clear_button.pack(pady=5)
    
    def perform_sort(self):
        input_str = self.entry.get()
        if not input_str:
            messagebox.showerror("错误", "请输入数组！")
            return
        
        try:
            nums = [int(num.strip()) for num in input_str.split(",")]
        except ValueError:
            messagebox.showerror("错误", "请输入有效的数字，用逗号分隔！")
            return
        
        # 执行排序
        solution = Solution()
        solution.wiggleSort(nums)
        
        # 显示结果
        self.result_text.config(state="normal")
        self.result_text.delete(1.0, tk.END)
        self.result_text.insert(tk.END, str(nums))
        self.result_text.config(state="disabled")
    
    def clear_fields(self):
        self.entry.delete(0, tk.END)
        self.result_text.config(state="normal")
        self.result_text.delete(1.0, tk.END)
        self.result_text.config(state="disabled")

if __name__ == "__main__":
    root = tk.Tk()
    app = WiggleSortApp(root)
    root.mainloop()
